package com.dailyblue.java.project.bookstore.listener;

import com.alibaba.fastjson.JSONArray;
import com.dailyblue.java.project.bookstore.bean.ShoppingCart;
import com.dailyblue.java.project.bookstore.bean.Stock;
import com.dailyblue.java.project.bookstore.service.StockService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;

import javax.annotation.Resource;
import java.util.List;

@RocketMQMessageListener(consumerGroup = "jiazhong_group_b", topic = "bookstore", selectorExpression = "order")
public class StockListener implements RocketMQListener<String> {
    @Resource
    private StockService service;

    @Override
    public void onMessage(String s) {
        List<ShoppingCart> carts = JSONArray.parseArray(s, ShoppingCart.class);
        carts.forEach(e -> {
            Stock stock = new Stock();
            stock.setNumber(e.getNumber());
            stock.setBookId(e.getBookId());
            service.updateStock(stock);
        });
    }
}
